package hu.stud.uszeged.inf.prf.prftrac.session.controllers;

import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;

import hu.stud.uszeged.inf.prf.prftrac.entity.Milestone;
import hu.stud.uszeged.inf.prf.prftrac.session.EventHome;
import hu.stud.uszeged.inf.prf.prftrac.session.MilestoneHome;
import hu.stud.uszeged.inf.prf.prftrac.session.UserHome;
import hu.stud.uszeged.inf.prf.prftrac.session.dao.MilestoneDao;
import hu.stud.uszeged.inf.prf.prftrac.session.dao.UserDao;
import hu.stud.uszeged.inf.prf.prftrac.session.enums.EntityType;
import hu.stud.uszeged.inf.prf.prftrac.session.enums.EventType;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.security.Credentials;

@Scope(ScopeType.EVENT)
@Name("milestoneController")
public class MilestonesController {
	
	@In("#{credentials}")
	private Credentials creds;
	@In("#{milestoneHome}")
	private MilestoneHome milestoneHome;
	@In("#{eventHome}")
	private EventHome eventHome;
	@In("#{userHome}")
	private UserHome userHome;
	@In("#{userDao}")
	private UserDao userDao;
	@In("#{milestoneDao}")
	private MilestoneDao milestoneDao;
	
	public List<Milestone> milestoneList() {
		userHome.setInstance(userDao.getUserByNickName(creds.getUsername()));
		return milestoneDao.getMilestonesByProject(userHome.getInstance().getProject());
	}
	
	public String persist() {
		String result = milestoneHome.persist();
		if (result.equals("persisted")) {
			eventHome.getInstance().setDateTime(new Date());
			eventHome.getInstance().setUser(userDao.getUserByNickName(creds.getUsername()));
			eventHome.getInstance().setType(EventType.NEW_MILESTONE);
			eventHome.getInstance().setEntityType(EntityType.MILESTONE);
			eventHome.getInstance().setEntityId(milestoneHome.getInstance().getId());
			eventHome.persist();
		}
		return result;
	}
	
	public String update() {
		String result = milestoneHome.update();
		if (result.equals("updated")) {
			eventHome.getInstance().setDateTime(new Date());
			eventHome.getInstance().setUser(userDao.getUserByNickName(creds.getUsername()));
			eventHome.getInstance().setType(EventType.UPDATE_MILESTONE);
			eventHome.getInstance().setEntityType(EntityType.MILESTONE);
			eventHome.getInstance().setEntityId(milestoneHome.getInstance().getId());
			eventHome.persist();
		}
		return result;
	}
	
	public String remove() {
		String result = milestoneHome.remove();
		milestoneHome.clearInstance();
		if (result.equals("removed")) {
			eventHome.getInstance().setDateTime(new Date());
			eventHome.getInstance().setUser(userDao.getUserByNickName(creds.getUsername()));
			eventHome.getInstance().setType(EventType.DELETE_MILESTONE);
			eventHome.getInstance().setEntityType(EntityType.MILESTONE);
			eventHome.getInstance().setEntityId(milestoneHome.getInstance().getId());
			eventHome.persist();
		}
		return result;
	}
}
